<template>
  <div class="dashboard-container">
    <el-container class="main-container">
      <el-header height="60px">
        <div class="header-left">
          <h1>在线考试系统 - 管理员控制台</h1>
        </div>
        <div class="header-right">
          <el-dropdown>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
                <el-dropdown-item @click="handleChangePassword">修改密码</el-dropdown-item>
              </el-dropdown-menu>
            </template>
            <el-button type="primary">
              {{ userInfo.username }} - {{ userInfo.roleName }}
              <el-icon>
                <ArrowDown />
              </el-icon>
            </el-button>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px" class="sidebar">
          <el-menu :default-openeds="['0', '1', '2', '3', '4', '5']" router>
            <el-sub-menu index="0">
              <template #title>
                <el-icon>
                  <Avatar />
                </el-icon>
                <span>用户管理</span>
              </template>
              <el-menu-item index="/admin/user/list">用户列表</el-menu-item>
              <el-menu-item index="/admin/user/add">添加用户</el-menu-item>
              <el-menu-item index="/admin/teacher/list">教师列表</el-menu-item>
              <el-menu-item index="/admin/teacher/add">添加教师</el-menu-item>
              <el-menu-item index="/admin/student/list">学生列表</el-menu-item>
              <el-menu-item index="/admin/student/add">添加学生</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="1">
              <template #title>
                <el-icon>
                  <Document />
                </el-icon>
                <span>课程管理</span>
              </template>
              <el-menu-item index="/admin/course/list">课程列表</el-menu-item>
              <el-menu-item index="/admin/course/add">添加课程</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="2">
              <template #title>
                <el-icon>
                  <OfficeBuilding />
                </el-icon>
                <span>班级管理</span>
              </template>
              <el-menu-item index="/admin/class/list">班级列表</el-menu-item>
              <el-menu-item index="/admin/class/add">添加班级</el-menu-item>
            </el-sub-menu>
            <el-sub-menu index="3">
              <template #title>
                <el-icon>
                  <Setting />
                </el-icon>
                <span>系统设置</span>
              </template>
              <el-menu-item index="/admin/course/allocation">课程分配</el-menu-item>
              <el-menu-item index="/admin/score/query">成绩查询</el-menu-item>
            </el-sub-menu>
          </el-menu>
        </el-aside>
        <el-main>
          <div class="content">
            <slot></slot>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { User, Document, OfficeBuilding, Setting, ArrowDown, Avatar, UserFilled } from '@element-plus/icons-vue'
import { get } from '../../utils/api'

const router = useRouter()

const userInfo = ref({
  username: '',
  roleName: ''
})

onMounted(() => {
  // 从本地存储获取用户信息
  const storedUserInfo = localStorage.getItem('userInfo')
  if (storedUserInfo) {
    userInfo.value = JSON.parse(storedUserInfo)
  }
})

const handleLogout = async () => {
  try {
    // 调用后端登出接口，带上cookie
    await get('/user/logout')
    
    // 清除本地存储的用户信息
    localStorage.removeItem('user')
    
    // 跳转到登录页面
    router.push('/login')
    
    ElMessage.success('退出登录成功')
  } catch (error) {
    console.error('退出登录失败:', error)
    // 即使后端调用失败，也要清除本地状态并跳转
    localStorage.removeItem('user')
    router.push('/login')
    ElMessage.success('退出登录成功')
  }
}

const handleChangePassword = () => {
  router.push('/admin/change-password')
}
</script>

<style scoped>
.dashboard-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  position: fixed;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 1;
  overflow: hidden;
}

.main-container {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}

.el-header {
  background-color: #1890ff;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.header-left h1 {
  font-size: 20px;
  margin: 0;
  font-weight: normal;
}

.sidebar {
  background-color: #f0f2f5;
  border-right: 1px solid #e8e8e8;
  height: 100%;
}

.el-main {
  padding: 20px;
  background-color: #f5f5f5;
  overflow-y: auto;
  height: 100%;
}

.content {
  min-height: 100%;
}
</style>